layui.use(['table','layer','form', 'layedit','jquery'], function(){
    var table = layui.table
        ,layer = layui.layer
        ,form = layui.form
        ,layedit = layui.layedit
        ,$ = layui.jquery;

    var body = "";
    var checkStatus;//多选框选中状态
    var checkedData = "";
    //判断是否有选中行，没有得话则把修改按钮置灰
    $(function(){
        //获取到选中数据
        checkedData = layui.table.checkStatus('wxSpell_table').data;
    });
    table.render({
        elem: '#wxSpell_table'
        ,url:'../wxSpell/querySpellGoodsRegimental'
        ,response: {
            statusName: 'code'
            ,statusCode: '100012'
            ,msgName: 'msg'
            ,countName: 'total'
            ,dataName: 'data'
        }
        ,toolbar: '#toolbar'
        ,title: '字典类型'
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field:'strattime', title:'开始时间',width:200, templet : "<div>{{layui.util.toDateString(d.strattime, 'yyyy-MM-dd HH:mm:ss')}}</div>", fixed: 'left'}
            ,{field:'count', title:'产品数量',fixed: 'left', align: 'center',templet:"#goodsCount",width:180}
            ,{field:'goods_type_name', title:'拼团人数', fixed: 'left',align: 'center',width:180}
            ,{field:'sales_price', title:'成团人数', fixed: 'left',align: 'center',width:180}
            ,{field:'clouds_num', title:'成团条件', fixed: 'left',align: 'center',templet:"#cttj",width:180}
            ,{field:'state', title:'状态', fixed: 'left',align: 'center',templet:"#state",width:180}
            ,{field:'endtime', title:'结束时间',width:200, templet : "<div>{{layui.util.toDateString(d.endtime, 'yyyy-MM-dd HH:mm:ss')}}</div>", fixed: 'left'}
           ,{fixed: 'right', title:'操作', toolbar: '#operations'
            }
        ]]
        ,page: true
        ,id: 'table'
        ,limit:15
        ,limits:[15,20,30,40]
        ,skin: 'line'
    });

    //打开弹出层表单
    function openForm(title,event,url1,url2,params){
        layer.open({
            type: 2,
            area:['70%', '60%'],
            fixed:true,
            anim: 2,
            offset: '100px',
            maxmin:false,
            scrollbar: false,
            title: title,
            content:url1, //[url1,url2],//['/page/addSpell.html','no'],
            end: function () {
                table.reload('table');
            },
            btn:['确认','取消'],
            success: function(layero, index) {
                //设置body为全局变量
                body = layer.getChildFrame('body',index);
                switch (event) {
                    case 'addSpell':
                        break;
                    case 'edit':
                        var gid="";
                        var goodsId="";
                        $.each($.parseJSON(params),function (i,v) {
                            gid=v.goods_type;
                            goodsId=v.id;
                        })
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.getWxGoodsType(gid);
                        iframeWin.getWxGoodsInfo(goodsId);
                        break;
                    case 'addGoods':
                        break;
                    case 'spellGoods':
                        break;
                }
            },
            yes: function(index, layero){
                switch (event) {
                    case 'addSpell':
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.addWxSpell();
                        break;
                    case 'edit':
                        //var data = checkStatus.data;
                        var goodsId="";
                        $.each($.parseJSON(params),function (i,v) {
                            goodsId=v.id;
                        });
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.updateWxGoods(goodsId);
                        break;
                    case 'addGoods':
                        layer.close(index);
                        table.reload('table');
                        break;
                    case 'spellGoods':
                        layer.close(index);
                        table.reload('table');
                        break;
                }
            }
            ,btn2: function(index, layero){
                layer.close(index);
                table.reload('table');
               //
                //table.reload('wxGoods_table');
                return false;
            }
        });
    }

    //头工具栏事件
    table.on('toolbar(wxSpell_table)', function(obj){
        //设置选中状态为全局变量
        checkStatus = table.checkStatus(obj.config.id);
        /*  这两行报错有问题  var index = parent.layer.getFrameIndex(window.name);
            parent.layer.iframeAuto(index);*/
        switch(obj.event){
            case 'getCheckData':
                var data = checkStatus.data;
                layer.alert(JSON.stringify(data));
                break;
            case 'getCheckLength':
                var data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
                break;
            case 'isAll':
                layer.msg(checkStatus.isAll ? '全选': '未全选');
                break;
            case 'addSpell':
                openForm('新增拼团信息','addSpell','/page/addSpell.html','no',null);
                break;
          /*  case 'addGoods':
                var spellId=data.id;
                openForm('拼团商品添加','addGoods',"/page/addSpellGoods.html?spellId="+spellId,"no",params);
                break;*/
        };
    });

    //监听行工具事件
    table.on('tool(wxSpell_table)', function(obj){
        var data = obj.data;
        //console.log(obj)
        /*  var ids=data.id+',';
          alert(ids);*/
        if(obj.event === 'del'){
            layer.confirm('是否删除选中用户',{offset: '320px'}, function(index){
                delWxGoodsById('../wxGoods/delWxGoodsById',data.id);
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            var array=new Array();
            array.push(data);
            var params=JSON.stringify(array);
            openForm('修改商品信息','edit',null,null,params);
        } else if(obj.event === 'addGoods')
        {
            var spellId=data.id;
            openForm('拼团商品添加','addGoods',"/page/addSpellGoods.html?spellId="+spellId,"no",params);
        }else if(obj.event === 'spellGoods')
        {
            var spellId=data.id;
            openForm('拼团商品添加','spellGoods',"/page/bdSpellGoods.html?spellId="+spellId,"no",params);
        }
    });


    $("#mh_spell").on('click',function () {
        mhSelect();
    });

    function mhSelect() {
        var strattime = $('#strattime').val();
        var endtime=$('#endtime').val();
        table.reload('table', {
            url: '../wxSpell/querySpellGoodsRegimental'
            ,where: {
                strattime : strattime,
                endtime : endtime
            }
            ,page: {
                curr: 1
            }
        });
    }




});

